package com.fyzk.activity.user.coupon;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.RadioButton;

import com.alibaba.fastjson.JSON;
import com.fyzk.base.BaseActivity;
import com.fyzk.activity.user.NetProgressView;
import com.fyzk.bean.CouponListBean;
import com.tyjc.lottery.R;
import com.fyzk.net.Commands;
import com.fyzk.view.RefreshListView;
import com.fyzk.view.TopTitleView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import http.Common.CommandUtil;
import http.StringResultBean;

/**
 * Created by fanxs11 on 17/1/23.
 *
 * 优惠券
 */
public class CouponActivity extends BaseActivity implements RefreshListView.OnRefreshListener{

    private TopTitleView topView;
    private RadioButton noUse_rb;       //未使用
    private RadioButton use_rb;         //已经使用
    private RadioButton overdue_rb;     //已经过期
    private RefreshListView listView;
    private CouponAdapter adapter;
    private NetProgressView recordProgressView;

    public static List<CouponListBean.CouponsBean> infos = new ArrayList<>();

    private static final int STATUS_NOUSER = 1;     // 未使用
    private static final int STATUS_USER = 2;       // 已使用
    private static final int STATUS_OVERDUE = 3;    // 已过期

    private int status = STATUS_NOUSER;

    private boolean requestType = false;    //请求方式：true加载更多，false首次请求或刷新
    private int page = 1;
    private int count = 10;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.coupon_layout);

        net(true);

        initView();
        Intent intent = getIntent();
        String data = intent.getDataString();
    }

    /**
     * 初始化控件
     */
    private void initView(){
        topView = (TopTitleView) findViewById(R.id.coupon_topview);
        topView.setCenterText(R.string.coupon_title);
        topView.setRightVisible(View.GONE);
        topView.setOnTitleClickListener(this);

//        noUse_rb = (RadioButton) findViewById(R.id.coupon_rb_nouse);
//        use_rb = (RadioButton) findViewById(R.id.coupon_rb_use);
//        overdue_rb = (RadioButton) findViewById(R.id.coupon_rb_overdue);

//        noUse_rb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//
//                if (b) {
//                    page = 1;
//                    status = STATUS_NOUSER;
//                    infos.clear();
//                    net(true);
//                }
//
//            }
//        });

//        use_rb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//                if (b) {
//                    page = 1;
//                    status = STATUS_USER;
//                    infos.clear();
//                    net(true);
//                }
//
//            }
//        });

//        overdue_rb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//                if (b) {
//                    page = 1;
//                    status = STATUS_OVERDUE;
//                    infos.clear();
//                    net(true);
//                }
//
//            }
//        });

        listView = (RefreshListView) findViewById(R.id.coupon_listview);
        listView.setItemsCanFocus(false);
        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        listView.setOnRefreshListener(this);
        adapter = new CouponAdapter(this);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (listView.isDownPull){
                    return;
                }
                if (i > 0 && i <= infos.size()) {
                    if (infos.get(i-1).getStatus().equals(""+STATUS_NOUSER)){
                        Intent intent = new Intent(getContext(), CouponDetail.class);
                        intent.putExtra("coupon", infos.get(i-1));
                        startActivity(intent);
                    }
                }


            }
        });

        recordProgressView = (NetProgressView) findViewById(R.id.recordProgressView);

    }


    @Override
    public void onDownPullRefresh() {
        page = 1;
        requestType = false;
        net(false);
    }

    @Override
    public void onLoadingMore() {
        requestType = true;
        page++;
        net(false);
    }

    // >>>>>>>>> net >>>>>>>>>

    private void requestCouponList(boolean showProgress) {
        JSONObject request_json = new JSONObject();
        try {
            request_json.put("status", this.status);    // 优惠劵状态1未使用 2使用 3过期
            request_json.put("pageIndex", page);
            request_json.put("maxResult", count);
            httpPostJson(CommandUtil.FINDUSERCOUPON, Commands.FINDUSERCOUPON, request_json,
                    showProgress);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onSuccess(int requestCode, StringResultBean resultBean, String result) {
        super.onSuccess(requestCode, resultBean, result);
        recordProgressView.dismiss();
        CouponListBean bean = JSON.parseObject(resultBean.getData(), CouponListBean.class);
        if (requestType) {  // 加载更多
            if (bean.getCoupons().size() > 0) {
                infos.addAll(bean.getCoupons());
                listView.hideFooterView();
            } else {
                listView.noMore();
            }
        } else {
            infos = bean.getCoupons();
            listView.hideHeaderView();
            if (infos.size() == 0) {
                listView.noMore();
            }
        }
        adapter.notifyDataSetChanged();
        requestType = false;
    }

    @Override
    protected void onFailure(int requestCode, String errorcode,String message) {
        if(errorcode.equals("900003")){ // 数据为空
            if (!requestType) { // 下拉刷新或第一次请求
                infos.clear();
                recordProgressView.noneRecord(message);
            } else {    // 加载更多
                listView.noMore();
            }
        }
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }

    private void net(boolean showProgress){
//        DJ_ProgressDialog.show(getContext(), true, true);
       requestCouponList(showProgress);
    }

}
